package com.social.instadownloader.screen;

import android.app.ProgressDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.social.instadownloader.R;
import com.social.instadownloader.activity.MainActivity;
import com.social.instadownloader.network.FetcherData;
import com.social.instadownloader.network.InstaVideoFetcher;
import com.social.instadownloader.network.InstaVideoFetcher.OnFetcherListener;
import com.social.instadownloader.network.download.InstaDownloader;
import com.social.instadownloader.screen.common.InstaFragmentScreen;
import com.social.instadownloader.util.ClipboardUtil;
import com.social.instadownloader.util.UniversalImageLoader;

public class DownloadScreen extends InstaFragmentScreen implements OnClickListener{

	private Button btnPasteUrl;
	private Button btnCheckUrl;
	private Button btnSaveImage;
	private Button btnSaveVideo;
	private ImageView imgImageFetcher;
	private ImageView imgVideoFetcher;
	private AutoCompleteTextView edtLink; 
	/** The m progress dialog. */
    private ProgressDialog mProgressDialog;
	private String currentUrl;
	/** The m current fetcher data. */
    private FetcherData mCurrentFetcherData;
	private InstaDownloader mInstaDownloader;
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.download_screen, container,false);
		btnPasteUrl = (Button) v.findViewById(R.id.btnPasteURL);
		btnCheckUrl = (Button) v.findViewById(R.id.btnCheckUrl);
		btnSaveImage = (Button) v.findViewById(R.id.btnSaveImage);
		btnSaveVideo = (Button) v.findViewById(R.id.btnSaveVideo);
		edtLink = (AutoCompleteTextView) v.findViewById(R.id.edtLink);
		imgImageFetcher = (ImageView) v.findViewById(R.id.imgImageFetcher);
		imgVideoFetcher = (ImageView) v.findViewById(R.id.imgVideoFetcher);
		
		mProgressDialog = new ProgressDialog(getActivity());
		mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		mInstaDownloader = new InstaDownloader(getActivity());
		btnPasteUrl.setOnClickListener(this);
		btnCheckUrl.setOnClickListener(this);
		btnSaveImage.setOnClickListener(this);
		btnSaveVideo.setOnClickListener(this);
		initParams();
		return v;
	}

	/**
     * Inits the params.
     * 
     * @param v the v
     */
    private void initParams() {
        imgImageFetcher.getLayoutParams().width = MainActivity.SCREEN_WIDTH / 3;
        imgImageFetcher.getLayoutParams().height = (int) (MainActivity.SCREEN_WIDTH / 2.5);
        imgVideoFetcher.getLayoutParams().width = MainActivity.SCREEN_WIDTH / 3;
        imgVideoFetcher.getLayoutParams().height = (int) (MainActivity.SCREEN_WIDTH / 2.5);
    }
    
	/**
     * On paste copied link.
     */
    private void onPasteCopiedLink() {
        hideKeyBoard(edtLink);
        String textInClipboard = ClipboardUtil.getText(getActivity());
        if (textInClipboard == null || textInClipboard.equals("")) {
            this.edtLink.setText("http://instagram.com/p/");
//            saveUrlShare("http://instagram.com/p/");
            return;
        }
        this.edtLink.setText(textInClipboard);
//        saveUrlShare(textInClipboard);
        startFetchingUrl(edtLink.getText().toString());
        currentUrl = edtLink.getText().toString();
    }
    
    /**
     * Hide key board.
     * 
     * @param paramView the param view
     */
    protected void hideKeyBoard(View paramView) {
        try {
            ((InputMethodManager) getActivity().getSystemService("input_method")).hideSoftInputFromWindow(
                    paramView.getWindowToken(), 0);
            return;
        } catch (Exception localException) {
            Log.e("InstaDownloadFragment", localException.toString());
        }
    }
    
    /**
     * Start fetching url.
     * 
     * @param string the string
     */
    public void startFetchingUrl(final String string) {
        InstaVideoFetcher localInstaVideoFetcher = new InstaVideoFetcher();
//    	VineVideoFetcher localInstaVideoFetcher = new VineVideoFetcher();
        String uRLTobeFetched = localInstaVideoFetcher.validate(string);
        if (!"".equals(uRLTobeFetched)) {
            showDialog("fetching url...");
            localInstaVideoFetcher.fetch(uRLTobeFetched, new OnFetcherListener() {

                @Override
                public void onFetcherFail(String message) {
                    mCurrentFetcherData = null;
                    dismissDialog();
                }

                @Override
                public void onFetcherComplete(FetcherData data) {
                    dismissDialog();
                    mCurrentFetcherData = data;
                    View[] vg = {imgImageFetcher, imgVideoFetcher};
                    UniversalImageLoader.getInstance().loadImageView(vg, data.getImageUrl(),
                            MainActivity.SCREEN_WIDTH / 3, 0);
                }
            });
            return;
        } else {
            Toast.makeText(getActivity(), "Invalid Url", Toast.LENGTH_SHORT).show();
        }
    }
    
    
    /**
     * Show dialog.
     * 
     * @param message the message
     */
    public void showDialog(String message) {
        if (!mProgressDialog.isShowing()) {
            mProgressDialog.setMessage(message);
            mProgressDialog.show();
        }
    }

    /**
     * Dismiss dialog.
     */
    public void dismissDialog() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

	@Override
	public String getScreenName() {
		return "Download";
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnPasteURL:
			onPasteCopiedLink();
			break;
		case R.id.btnCheckUrl:
			if (edtLink.getText() != null) {
                startFetchingUrl(edtLink.getText().toString());
            }
			break;
		case R.id.btnSaveImage:
			mInstaDownloader.downloadImage(mCurrentFetcherData);
			break;
		case R.id.btnSaveVideo:
			mInstaDownloader.downloadVideo(mCurrentFetcherData);
			break;
		default:
			break;
		}
	}

}
